package com.hspedu.houserent.view;

import com.hspedu.houserent.HouseRentApp;
import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;

/**
 * 显示界面
 * 接收用户输入
 * 调用HouseService完成对房屋信息的操作
 */
public class HouseView {
    private boolean loop = true; //控制显示主菜单
    private  char key = ' '; //接受用户选择
    private HouseService houseService = new HouseService(10); //设置大小为10

    public void corrHouse()
    {
        System.out.println("修改信息");
        System.out.println("请输入编号");
        int repId = Utility.readInt();
        int rep = houseService.rep(repId);
        System.out.println(houseService.printHouse(rep));

        System.out.println("姓名：");
        String name = Utility.readString(8);
        System.out.println("电话：");
        String phone = Utility.readString(12);
        System.out.println("地址：");
        String address = Utility.readString(16);
        System.out.println("月租：");
        int rent = Utility.readInt();
        System.out.println("状态：");
        String stats = Utility.readString(3);

        houseService.corr(rep, new House(0,name,phone,address,rent,stats));

    }
    //查找
    public void repHouse()
    {
        System.out.println("查找信息");
        System.out.println("请输入编号");
        int repId = Utility.readInt();
        int rep = houseService.rep(repId);
        System.out.println(houseService.printHouse(rep));
    }
    //删除房屋信息
    public void delHouse()
    {
        System.out.println("删除信息");
        System.out.println("请输入编号(-1退出)");
        int delId = Utility.readInt();
        if(delId == -1)
        {
            System.out.println("你放弃了删除");
            return;
        }

        char choices = Utility.readConfirmSelection();
        if(choices == 'Y')
        {
            boolean del = houseService.del(delId);
            if(!del)
            {
                System.out.println("删除失败");
            }
        }
        else
        {
            System.out.println("你放弃了删除");
            return;
        }
    }
    //添加房屋信息
    public void addHouse()
    {
        System.out.println("添加房屋");
        System.out.println("姓名：");
        String name = Utility.readString(8);
        System.out.println("电话：");
        String phone = Utility.readString(12);
        System.out.println("地址：");
        String address = Utility.readString(16);
        System.out.println("月租：");
        int rent = Utility.readInt();
        System.out.println("状态：");
        String stats = Utility.readString(3);

        boolean addHouse = houseService.addHouse(name, phone, address, rent, stats);
        if(!addHouse)
        {
            System.out.println("无法添加更多");
        }

    }

    //显示房屋列表
    public void listHouses()
    {
        System.out.println("房屋列表");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] list = houseService.list();
        for (House house : list) {
            if(house == null)
                break;
            System.out.println(house);
        }
    }
    //显示主菜单
    public void mainMenu(){
        do {
            System.out.println("房屋出租系统菜单");
            System.out.println("\t\t\t1 新 增 房 屋");
            System.out.println("\t\t\t2 查 找 房 源");
            System.out.println("\t\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退     出 ");
            System.out.println("请输入你的选择(1-6)");
            key = Utility.readChar();
            switch (key)
            {
                case '1':
                    System.out.println("新 增");
                    addHouse();
                    break;
                case '2':
                    System.out.println("查 找");
                    repHouse();
                    break;
                case '3':
                    System.out.println("删 除");
                    delHouse();
                    break;
                case '4':
                    System.out.println("修 改");
                    corrHouse();
                    break;
                case '5':
                    System.out.println("房 屋 列 表");
                    listHouses();
                    break;
                case '6':
                    System.out.println("退 出");
                    loop = false;
                    break;

            }
        }while (loop);
    }
}
